package com.jiu.http.fish;

import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import com.jiu.core.FishClient;
import com.jiu.core.FishContext;
import com.jiu.util.XmlUtils;

public class FishHttpService {
	private FishContext context;
	public FishHttpService(FishClient context) {
		this.context = context;
	}

	public void sendFishMsg(String msg) {
		try {			
//			System.out.println(HttpContent.PutFishingCmd(context.getAccount(), msg));
			FishHttp.PutFishingCmd(context.getAccount(), msg);
		} catch (IOException e) {
			try {
				FishHttp.PutFishingCmd(context.getAccount(), msg);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	public String getFishMsg(int id){		
		return FishHttp.GetQMsg(context.getAccount(),id);
	}
	public void LogIn(){		
		FishHttp.LogIn(context.getAccount());		
	}
	public void LogOut() {
		FishHttp.LogOut(context.getAccount());
	}
	/** 背包
	 * @return
	 */
	public List<Map<String,Object>> getFishBag(){
		List<Map<String,Object>> retList = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = null;
		try {
			if(context.getAccount().getGroupid()==null)throw new Exception("还未捕获密码串！");
			//获取并解析 背包
			//HttpConnection.fishingConnection(HttpConnection.URL,HttpContent.GetFishingBagUsedSlot,HttpContent.GetFishingBagUsedSlot(context.getAccount()));
			//String httpRet= HttpConnection.fishingConnection(HttpConnection.URL,HttpContent.GetFishingBagTable,HttpContent.GetFishingBagTable(context.getAccount()));
			List<Map<String,String>> list = getBagList();
			int count = 0;
			for(Map<String,String> m:list){
				if(!m.get("name").contains("▲"))count++;
			}
			map = new HashMap<String,Object>();
			map.put("color", new Color(123,123,255));
			map.put("size", 13);
			map.put("msg", "\n－－－－－－－－－－－－－－－【背　包】－－【已用 "+count+" 格】－－－－－－－－－－－－－－－－\n");
			retList.add(map);//添加分隔线
			retList.addAll(parseFishBagAndBankTable(list));	//添加背包物品			
			
			//获取并解析 保险箱
			//HttpConnection.fishingConnection(HttpConnection.URL,HttpContent.GetFishingBankUsedSlot,HttpContent.GetFishingBankUsedSlot(context.getAccount()));
			//httpRet = HttpConnection.fishingConnection(HttpConnection.URL,HttpContent.GetFishingBankTable,HttpContent.GetFishingBankTable(context.getAccount()));	
			list =  getBankList();			
			map = new HashMap<String,Object>();
			map.put("color", new Color(255,133,123));
			map.put("size", 13);
			map.put("msg", "\n－－－－－－－－－－－－－－－【保险箱】－－【已用 "+list.size()+" 格】－－－－－－－－－－－－－－－－\n");
			retList.add(map);//添加分隔线
			retList.addAll(parseFishBagAndBankTable(list));	//添加保险箱物品					
	
			map = new HashMap<String,Object>();
			map.put("color", new Color(123,255,123));
			map.put("size", 13);
			map.put("msg", "\n－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－\n");
			retList.add(map);//添加分隔线
		}catch (IOException e) {
			e.printStackTrace();
		}catch (Exception e) {
			JOptionPane.showMessageDialog(context.getMainFrame(), "未捕获到钓鱼客户端密码串，请先登录客户端！");
		}
		return retList;
	}
	/**个人详细信息
	 * @return
	 */
	public List<Map<String,Object>> getFishMyDetail(){
		List<Map<String,Object>> retList = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = null;
		String info = FishHttp.GetDetailInfo(context.getAccount());	
		Pattern pattern = Pattern.compile("<GetDetailInfoResult>(.*)</GetDetailInfoResult>");
		Matcher matcher = pattern.matcher(info);
		if(matcher.find()){
			String[] infos = matcher.group(1).split("\\\\\\\\n");
			for(String msg:infos){
				map = new HashMap<String,Object>();
				map.put("size", 13);
				map.put("msg", msg+"\n");
				if(msg.contains(context.getAccount().getNickname())){
					map.put("color", Color.PINK);
				}else if(msg.contains("★")){
					map.put("color", new Color(220,220,2));
				}else if(msg.contains("※")){
					map.put("color", new Color(188,188,18));
				}else{
					map.put("color", new Color(80,200,80));
				}
				retList.add(map);
			}
		}
		//retVal += HttpConnection.fishingConnection(HttpConnection.URL,HttpContent.GetMe,HttpContent.GetMe(context.getAccount()));	
		return retList;
	}
	/**拍卖场
	 * @param type
	 * @return
	 */
	public List<Map<String,Object>> getFishAuction(String type){
		List<Map<String,Object>> retList = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("color", new Color(123,123,255));
		map.put("size", 13);
		
		String info = FishHttp.GetAuction(context.getAccount(),type);	
		Pattern pattern = Pattern.compile("<GetAuctionResult>(.*)</GetAuctionResult>");
		Matcher matcher = pattern.matcher(info);
		if(matcher.find()){
			String[] infos = matcher.group(1).split("\\\\\\\\n");
			for(String msg:infos){
				map = new HashMap<String,Object>();
				map.put("size", 13);
				map.put("msg", msg+"\n");
				map.put("color", randomColor());
				retList.add(map);
			}
		}					
		return retList;
	}


	
	/**自动找背包的食物
	 * @return
	 */
	public String getFood() {		
		String food = "";
			List<Map<String,String>> list = getBagList();
			for(Map<String,String> m:list){
				if(m.get("type").equals("5")){
					food = m.get("name");
			}						
		}
		return food;
	}

	/** 是否有鱼饵
	 * @return
	 */
	public boolean hasFishEr() {
		boolean hasEr = false;
		List<Map<String,String>> list =  getBagList();
		for(Map<String,String> m:list){
			if(m.get("type").equals("2")&&!m.get("name").contains("{△")){
				hasEr = true;
				break;
			}
		}					
		return hasEr;		
	}
	public List<Map<String,String>> getBagList(){
		String httpRet = FishHttp.GetFishingBagTable(context.getAccount());		
		Pattern pattern = Pattern.compile("<diffgr:diffgram(.*)</diffgr:diffgram>");
		Matcher matcher = pattern.matcher(httpRet);
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		if(matcher.find()){ 
			list =  XmlUtils.parseFishBagAndBankTableXml(matcher.group());		
		}
		return list;
	}
	public List<Map<String,String>> getBankList(){
		String httpRet = FishHttp.GetFishingBankTable(context.getAccount());
		Pattern pattern = Pattern.compile("<diffgr:diffgram(.*)</diffgr:diffgram>");
		Matcher matcher = pattern.matcher(httpRet);
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		if(matcher.find()){ 
			list =  XmlUtils.parseFishBagAndBankTableXml(matcher.group());		
		}
		return list;
	}
	private List<Map<String,Object>> parseFishBagAndBankTable(List<Map<String, String>> list) {
		List<Map<String,Object>> retList = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = null;
		for(Map<String,String> m:list){
			map = new HashMap<String,Object>();
			map.put("size", 13);
			if(!m.get("count").equals("1")){
				map.put("msg", m.get("name")+"*"+m.get("count")+"  ");
			}else{
				map.put("msg", m.get("name")+"  ");
			}
			int type = Integer.valueOf(m.get("type"));
			Color color = null;
			switch(type){
				case 1 : if(m.get("name").contains("〖"))
							color = new Color(188,188,222);
						 else
							color = new Color(133,133,133);break;
				case 2 : color = new Color(205,205,150);break;
				case 3 : color = new Color(22,245,22);break;
				case 4 : if(m.get("name").contains("〖"))
							color = new Color(188,188,222);
				 		 else
				 			color = new Color(155,155,155);break;
				case 5 : color = new Color(200,245,126);break;
				case 6 : color = new Color(60,126,235);break;
				case 7 : color = new Color(0,235,0);break;
				case 8 : color = new Color(22,245,22);break;
				case 9 : color = new Color(0,235,123);break;
				case 10 : color = new Color(22,245,22);break;
				case 11 : color = new Color(22,245,22);break;
				case 12 : color = new Color(22,245,22);break;
				case 13 : color = new Color(22,245,22);break;
				case 14 : color = new Color(200,20,200);break;
				case 15 : color = new Color(22,245,22);break;
				case 16 : color = new Color(68,201,201);break;
				case 17 : color = new Color(136,187,221);break;
				case 18 : color = new Color(22,245,22);break;
				case 19 : color = new Color(22,245,22);break;
				case 20 : color = new Color(22,245,22);break;
				case 21 : color = new Color(0,255,0);break;
				case 22 : color = new Color(22,245,22);break;
				case 23 : color = new Color(22,245,22);break;
				case 26 : color = new Color(0,205,0);break;
				case 28 : color = new Color(0,210,0);break;
				case 44 : color = new Color(0,225,0);break;
				case 45 : color = new Color(0,235,0);break;
				case 46 : color = new Color(0,245,0);break;
				case 58 : color = new Color(0,255,153);break;
				case 71 : color = new Color(0,188,0);break;
				case 80 : color = new Color(222,222,88);break;
				case 99 : color = new Color(255,156,18);break;
				default : color = new Color(22,245,22);break;
			}
			map.put("color", color);
			retList.add(map);
		}
		return retList;
	}
	private Color randomColor(){
		Random random = new Random();
		return new Color(random.nextInt(200)+50,random.nextInt(180)+70,random.nextInt(160)+90);
	}

	
}
